package cn.jinjimi.app.custom.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.dialog.CustomAddPhoneTypeDialog;

/**
 * 添加电话号码
 * Created by Jinsen on 2015/6/25.
 */
public class CustomAddPhoneView extends RelativeLayout implements View.OnClickListener, CustomPhoneDelWindow.onActionClickListener {

    Context mContext = null;
    EditText edPhone = null;
    TextView tvType = null;
    View emptyView;
    ViewGroup mParentView = null;
    int mType = 0;
    CustomAddPhoneTypeDialog dialog;

    private CustomPhoneDelWindow customPhoneDelWindow;

    public CustomAddPhoneView(Context context, ViewGroup parent) {
        this(context, null, 0);
        mParentView = parent;
        dialog = new CustomAddPhoneTypeDialog(context);
    }

    public CustomAddPhoneView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomAddPhoneView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.view_custom_add_phone_layout, this);
        mContext = context;
        initView();
    }

    private void initView() {
        edPhone = (EditText) findViewById(R.id.ed_phone);
        tvType = (TextView) findViewById(R.id.tv_type);
        emptyView = findViewById(R.id.view_empty);

        findViewById(R.id.ll_title).setOnClickListener(this);
        findViewById(R.id.ll_del).setOnClickListener(this);
    }

    public String getPhone() {
        return edPhone.getText().toString().trim();
    }

    public void setPhoneType(int type, String name) {
        mType = type;
        tvType.setText(name);
    }

    public void setPhone(String phone) {
        edPhone.setText(phone);
    }

    public int getPhoneType() {
        return mType;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_del:
                showPopupWindow();
                break;

            case R.id.ll_title:
                dialog.setListener(mListener);
                dialog.show(getPhoneType());
                break;
        }
    }

    CustomAddPhoneTypeDialog.SelectPhoneTypeListener mListener = new CustomAddPhoneTypeDialog.SelectPhoneTypeListener() {
        @Override
        public void selectType(int index, String name) {
            if (mType > -1) {
                dialog.removeType(mType);
            }
            setPhoneType(index, name);
        }
    };

    private void showPopupWindow() {
        if (customPhoneDelWindow == null) {
            customPhoneDelWindow = new CustomPhoneDelWindow(mContext, this);
        }

        customPhoneDelWindow.showAsDropDown(emptyView);
    }

    @Override
    public void onDelClick() {
        if (mParentView != null)
            mParentView.removeView(CustomAddPhoneView.this);

        dialog.removeType(mType);

    }
}
